import { queryNotices } from '@/services/user';

const GlobalModel = {
  // 命名空间名字
  namespace: 'global', 
  state: {
    // 初始值
    collapsed: false,
    notices: [],
  },
  effects: {
    // 与后台交互处理逻辑的地方，没有参数
    *fetchNotices(_, { call, put, select }) {
      // data是从后台返回来的数据，可以传参过去
      const data = yield call(queryNotices);
      // put用来触发下面reducer里面的saveNotices方法也能触发自身同级的方法只不过传参要求不同，payload是传过去的参数对象{}[这个对象有个注意事项]，把处理得到新数据交给reducer替换新的state
      yield put({
        type: 'saveNotices',
        payload: data,
      });
      // //select就是用来选择上面state里的notices，然后把这个数据进行处理
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length,
      );
      // // 这里就会触发models层里面effects中fetchNum方法（也可以直接触发reducer中方法，看具体情况） ,user就是models里user.js文件里user的命名空间名字里的reducer的方法changeNotifyCount更新数据
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: data.length,
          unreadCount,
        },
      });
    },

    *clearNotices({ payload }, { put, select }) {
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
      const count = yield select(state => state.global.notices.length);
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length,
      );
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: count,
          unreadCount,
        },
      });
    },

    *changeNoticeReadState({ payload }, { put, select }) {
      const notices = yield select(state =>
        state.global.notices.map(item => {
          const notice = { ...item };

          if (notice.id === payload) {
            notice.read = true;
          }

          return notice;
        }),
      );
      yield put({
        type: 'saveNotices',
        payload: notices,
      });
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: notices.length,
          unreadCount: notices.filter(item => !item.read).length,
        },
      });
    },
  },
  reducers: {
    // 能改变界面的action放这里，不做数据处理。只用来返回新的state从而改变界面
    changeLayoutCollapsed(
      // 上面的初始值
      state = {
        notices: [],
        collapsed: true,
      },
      // 这个是payload是传过来的参数，是个对象，没参数可以写成{——，{call,put.select}}
      { payload },
    ) {
      // 覆盖前面的state里面的新值
      return { ...state, collapsed: payload };
    },
     
    saveNotices(state, { payload }) {
      return {
        collapsed: false,
        ...state,
        notices: payload,
      };
    },

    saveClearedNotices(
      state = {
        notices: [],
        collapsed: true,
      },
      { payload },
    ) {
      return {
        collapsed: false,
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },
  // 订阅监听，监听路由，进入页面就如何就在这写
  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      history.listen(({ pathname, search }) => {
        // pathname当前路由路径
        if (typeof window.ga !== 'undefined') {
          window.ga('send', 'pageview', pathname + search);
        }
      });
    },
  },
};
export default GlobalModel;
